package com.shine.icis.action;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.icis.blo.IMetadataCheckLogService;
import com.shine.icis.blo.ISubmitConfService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.DictOption;
import com.shine.icis.hbean.MetadataCheckItem;
import com.shine.icis.hbean.MetadataCheckLog;

public class MetadataCheckLogAction extends BaseAction {

	/**
	 * 元数据质量检查日志查询
	 * @author zhangxr 2010-10-22
	 */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private IMetadataCheckLogService metadataCheckLogService;
	private ISubmitConfService submitConfService;
	
	//input param
	private int pageNo;
	private int pageSize;
	private	String operDate;//日期
	private	String checkItemID;//类别
	private	String prepLevelID;//警告级别
	private String expType;//导出方式
	
	//output param
	private List<MetadataCheckItem> metadataCheckItems;   //类别
	private Collection<DictOption> prepLevelOptions;           //日志级别
	
	private ListPage<MetadataCheckLog> metadataCheckLogPage;
	private String previousTradeDate;//上一个交易日
	
	private String downloadUrl;//下载地址

	public void setMetadataCheckLogService(
			IMetadataCheckLogService metadataCheckLogService) {
		this.metadataCheckLogService = metadataCheckLogService;
	}
	public void setSubmitConfService(ISubmitConfService submitConfService) {
		this.submitConfService = submitConfService;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setOperDate(String operDate) {
		this.operDate = operDate;
	}


	public void setCheckItemID(String checkItemID) {
		this.checkItemID = checkItemID;
	}

	public void setPrepLevelID(String prepLevelID) {
		this.prepLevelID = prepLevelID;
	}

	public void setExpType(String expType) {
		this.expType = expType;
	}
	
	public ListPage<MetadataCheckLog> getMetadataCheckLogPage() {
		return metadataCheckLogPage;
	}
	
	public String getPreviousTradeDate() {
		return previousTradeDate;
	}
	
	public List<MetadataCheckItem> getMetadataCheckItems() {
		return metadataCheckItems;
	}

	public Collection<DictOption> getPrepLevelOptions() {
		return prepLevelOptions;
	}


	public String getDownloadUrl() {
		return downloadUrl;
	}
	/**
	 * 转到元数据质量检查日志页面
	 * @return
	 */
	public String initMetadataCheckLog() {
		metadataCheckItems = this.metadataCheckLogService.getMetadataCheckItems(MetadataCheckItem.ON);
		prepLevelOptions = SysCodeDictLoader.getInstance().getPrepLevelOptions();
		previousTradeDate = submitConfService.getPreviousTradeDate();
		return success();
	}
	
	
	/**
	 * 元数据质量检查日志
	 * @return
	 */
	public String queryMetadataCheckLog() {
		QueryParameters qp = new QueryParameters();
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		
		//增加对排序列的处理
		qp.addOrder("operDate", false);
		qp.addOrder("operTime", false);
		qp.addOrder("logID", false);
		
		//设置查询参数
		if(operDate != null){
			operDate = operDate.trim().replaceAll("-", "");
			qp.addParameter("operDate", Integer.parseInt(operDate));
		}
		if(prepLevelID != null && !prepLevelID.trim().equals("")){
			qp.addParameter("prepLevelID", Integer.parseInt(prepLevelID));
		}
		if(checkItemID != null && !checkItemID.trim().equals("")){
			qp.addParameter("checkItemID", Integer.parseInt(checkItemID));
		}
		try {
			metadataCheckLogPage = metadataCheckLogService.queryMetadataCheckLogPage(qp);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 导出Excel
	 */
	public String exportMetadataCheckLog() {
		QueryParameters qp = new QueryParameters();
		//增加对排序列的处理
		qp.addOrder("operDate", false);
		qp.addOrder("operTime", false);
		qp.addOrder("logID", false);
		
		//设置查询参数
		if(operDate != null){
			operDate = operDate.trim().replaceAll("-", "");
			qp.addParameter("operDate", Integer.parseInt(operDate));
		}
		if(prepLevelID != null && !prepLevelID.trim().equals("")){
			qp.addParameter("prepLevelID", Integer.parseInt(prepLevelID));
		}
		if(checkItemID != null && !checkItemID.trim().equals("")){
			qp.addParameter("checkItemID", Long.parseLong(checkItemID));
		}
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			String temFileName = this.metadataCheckLogService.csExpDataMetadataCheckLog(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}

}
